package com.tulei.shuqi;

import com.tulei.common.utils.file.ExcelHandler;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Thread.sleep;

/**
 * @Author TuLei
 * @Date 2024-07-09 13:10
 **/

public class ReadShuQi {

    public static void main(String[] args) throws InterruptedException {

        List<Book> list = new ArrayList<>();
        for (int i = 1; i <5; i++) {
            list.addAll(extracted(i));
            sleep(1000);
        }
        new ExcelHandler().writeExcel( "books.xlsx", list);
    }

    private static List<Book> extracted(int i) {
        List<Book> list = new ArrayList<>();
        Connection connect = Jsoup.connect("https://www.shuqi.com/store?&sz=0&page=" + i );
        try {
            Document document = connect.get();
            Elements elements = document.getElementsByClass("store-ul clear");
            Elements liElements = elements.get(0).getElementsByTag("li");
            for (Element liElement : liElements) {
                Element bookElement = liElement.selectFirst("li a.clear");

                if (bookElement == null) {
                    continue;
                }

                Book book = new Book();

                try {
                    String url = "https://www.shuqi.com" + bookElement.attr("href");
                    book.setWordCount(getWordCount(url));
                } catch (Exception e) {
                    book.setWordCount("N/A");
                }

                // 获取title，如果为空则设为"N/A"
                String title = bookElement.attr("title");
                book.setTitle(title.isEmpty() ? "N/A" : title);

                Element imgElement = bookElement.selectFirst("img.cover");
                if (imgElement != null) {
                    // 图片URL通常不会是空字符串，所以这里不作额外检查
                    book.setImageUrl(imgElement.attr("src"));
                } else {
                    // 如果imgElement为null，可以设imageUrl为"N/A"
                    book.setImageUrl("N/A");
                }

                Element desElement = bookElement.selectFirst("p.store-des");
                if (desElement != null) {
                    // 获取description，如果为空则设为"N/A"
                    String description = desElement.text();
                    book.setDescription(description.isEmpty() ? "N/A" : description);
                } else {
                    book.setDescription("N/A");
                }

                Elements categoryElements = liElement.select(".bkcate-icon");
                if (!categoryElements.isEmpty()) {
                    // 获取category，如果为空则设为"N/A"
                    String category = categoryElements.first().text();
                    book.setCategory(category.isEmpty() ? "N/A" : category);
                } else {
                    book.setCategory("N/A");
                }

                Elements authorElement = liElement.select(".bkuser-icon");
                if (!authorElement.isEmpty()) {
                    // 获取author，如果为空则设为"N/A"
                    String author = authorElement.first().text();
                    book.setAuthor(author.isEmpty() ? "N/A" : author);
                } else {
                    book.setAuthor("N/A");
                }

                list.add(book);
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return list;
    }
    private static String getWordCount(String url) {
        Connection connect = Jsoup.connect(url);
        Document document = null;
        try {
            document = connect.get();
        } catch (IOException e) {
            System.out.println("111");
        }
        Elements elements = document.getElementsByClass("lastchapter clear");
        Elements li = elements.get(0).select("li");
        return li.get(1).text();
    }
}
